/**
 *******************************************************************************
 * @FileName  : sdp.h
 * @Author    : GaoQiu
 * @CreateDate: 2020-05-18
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#ifndef SDP_H_
#define SDP_H_

#include "utils/defs_types.h"
#include "stack/host/dm_api.h"
#include "stack/host/att_api.h"

#if 1

#define APP_NEW_SDP_EN             1

#define APP_MAX_SERVICE_NUM        8
#define APP_MAX_CHAR_DECLARE_NUM   (3*4)//(4*6)

typedef struct{
	uint16_t  connHandle;
	uint8_t   evtId;
}SdpEvt_t;

enum{
	SDP_EVT_SERVER_SEARCH_START,
	SDP_EVT_SERVER_SEARCH_END,

	SDP_EVT_CHAR_DECLARE_SEARCH_START,
	SDP_EVT_CHAR_DECLARE_SEARCH_END,

	SDP_EVT_CCCD_SEARCH_START,
	SDP_EVT_CCCD_SEARCH_END,
	SDP_EVT_CCCD_SETTING_START,

	SDP_EVT_TIMEOUT,

	SDP_EVT_SDP_END,
};

typedef void (*SDP_EventCback_t)(SdpEvt_t *pEvt);

/*! SDP state. */
enum{
	SDP_STA_NONE = 0,
	SDP_STA_END  = 0,

	SDP_STA_SEARCH_SERVICE_START,
	SDP_STA_SEARCH_SERVICE_WAIT,

	SDP_STA_SEARCH_CHAR_START,
	SDP_STA_SEARCH_CHAR_WAIT,

	SDP_STA_SEARCH_CCCD_START,
	SDP_STA_SEARCH_CCCD_WAIT,

	SDP_STA_SET_CCC_START,
};

typedef struct{
	uint16_t     attHandle;     /* Characteristic Declare Handle */
	uint8_t      prop;
	uint8_t      align4;

	uuid_t       valueUuid;     /* Characteristic Value UUID */
	uint16_t     valueHandle;   /* Characteristic Value Handle */

	uint16_t     cccdHandle;
}Att_t;

typedef struct{
#if APP_NEW_SDP_EN
	Att_t        *pAtts;
#else
	Att_t         atts[APP_MAX_CHAR_DECLARE_NUM];
#endif
	uint16_t      attsCnt;
	uint16_t      maxAttNum;

	uuid_t        svcUuid;       /*!< Service UUID */
	uint16_t      startHandle;
	uint16_t      endHandle;
}AttGroup_t;

/*! SDP Controller Block. */
typedef struct{
	AttGroup_t        attGroup[APP_MAX_SERVICE_NUM];
	uint16_t          attGrpCnt;

	uint16_t          connHandle;

	uint16_t          attHandleStart; /* use during SDP */
	uint16_t          attHandleEnd;   /* use during SDP */

	uint32_t          timeoutTimer;   /* unit: ticks */

	uint8_t           curGrpCnt;  /*!< use for characteristic discovery */
	uint8_t           curAttCnt;  /*!< use for CCCD discovery */

	uint8_t           sdpState;
	uint8_t           enable;
}SdpCb_t;

void SDP_RegisterEvtCback(SDP_EventCback_t cback);

void SDP_Init(SdpCb_t *pSdpCb, uint16_t connHandle);
void SDP_ParamReset(SdpCb_t *pSdpCb);

void SDP_Start(SdpCb_t *pSdpCb);
void SDP_GattDataHandler(SdpCb_t *pSdpCb, uint8_t *pAttSdu, uint16_t len);
void SDP_GattErrRspHandler(SdpCb_t *pSdpCb, uint8_t *pAttSdu, uint32_t len);

AttGroup_t *SDP_FindService(SdpCb_t *pSdpCb, uint16_t attHandle);
Att_t *SDP_FindChar(SdpCb_t *pSdpCb, uint16_t attHandle);

void SDP_Handler(SdpCb_t *pSdpCb);
#endif




#endif /* SDP_H_ */
